package crx.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import crx.dao.BaseDao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.request = request;
		this.response = response;
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		int id = getInt("id");
		try {
			if ("selectMyMessage".equals(request.getParameter("type"))) {
				String sql = "select * from user where userid=?";
				Map<String, Object> query = BaseDao.query.query(sql, new MapHandler(), id);
				response.getWriter().write(gson.toJson(query));
			} else if ("selectMyFans".equals(request.getParameter("type"))) {
				String sql = "select u1.username as uname,u1.userintroduction as introduction,u1.a as imgname, u1.userid as uid from user u1 where userid in (select c.beconcerneduserid as cid from `user` s ,concern c where s.userid=c.userid and s.userid=?) ";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), id);
				response.getWriter().write(gson.toJson(query));
			} else if("selectMyFansTop".equals(request.getParameter("type"))) {
				String sql = "select u1.a as imgname, u1.userid as uid from user u1 where userid in (select c.beconcerneduserid as cid from `user` s ,concern c where s.userid=c.userid and s.userid=?) limit 1,4";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), id);
				response.getWriter().write(gson.toJson(query));
			}else if("selectMyConcernTop".equals(request.getParameter("type"))) {
				String sql = "select u1.a as imgname, u1.userid as uid from user u1 where userid in (select c.userid as concernid from user s, concern c where s.userid=c.beconcerneduserid and s.userid=?) limit 1,4";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), id);
				response.getWriter().write(gson.toJson(query));
			}else if("selectMyConcAll".equals(request.getParameter("type"))) {
				String sql =" select * from user u1 where userid in(select c.userid as concernid from user s, concern c where s.userid=c.beconcerneduserid and s.userid=?)";
				List<Map<String, Object>> query = BaseDao.query.query(sql,new MapListHandler(),id);
				response.getWriter().write(gson.toJson(query));
			}else if("selectMyMessageAll".equals(request.getParameter("type"))) {
				String sql="select m.messagecontent as messagecontent, m.messagetime as time, \r\n" + 
						"u.username as name, u.a as imgname, u.userid as uid\r\n" + 
						"from message m, user u where u.userid=m.messagesenderid and messagereceiverid=?\r\n" + 
						"order by m.messagetime desc";
				List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(),id);
				response.getWriter().write(gson.toJson(query));
			}else if("deleteMyFans".equals(request.getParameter("type"))) {
				Integer id1=getInt("id1");
				Integer id2=getInt("id2");
				String sql = "delete from concern where userid=? and beconcerneduserid=?";
				Integer i = BaseDao.query.update(sql,id1,id2);
				response.getWriter().write(gson.toJson(i));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
